package com.xxtx.trade.common.util;

import com.alibaba.fastjson.JSON;
import com.xxtx.trade.common.BWMD5Util;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 *网络请求工具示例，客户可自行选择其他请求工具
 */
public class BWHttpUtil {
	
	public static String sendPost(String uri, String reqJson, String timeStamp,String appid,String secretKey) {  
        String result = null;  
        PrintWriter out = null;  
        InputStream in = null;  
        try {
            //忽略安全证书
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            });
            URL url = new URL(uri);  
            HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();  
            urlcon.setRequestMethod("POST");  
            urlcon.setDoInput(true);
            urlcon.setDoOutput(true);
            //请求头信息
            urlcon.setRequestProperty("Content-Type", "application/json");
            urlcon.setRequestProperty("timeStamp", timeStamp);
            urlcon.setRequestProperty("appid", appid);
            String sign = BWMD5Util.stringToMD5(appid,secretKey,timeStamp);
            urlcon.setRequestProperty("sign", sign);
            urlcon.connect();// 获取连接  
            out = new PrintWriter(urlcon.getOutputStream());  
            out.print(reqJson);  
            out.flush();  
            in = urlcon.getInputStream();  
            BufferedReader buffer = new BufferedReader(new InputStreamReader(  
                    in, "utf-8"));  
            StringBuffer bs = new StringBuffer();  
            String line = null;  
            while ((line = buffer.readLine()) != null) {  
                bs.append(line);  
            }  
            result = bs.toString(); 
        } catch (Exception e) {  
        	System.out.println("请求异常:"+e.getMessage());
        	e.printStackTrace();
        } finally {  
            try {  
                if (null != in)  
                    in.close();  
                if (null != out)  
                    out.close();  
            } catch (Exception e2) {
            	System.out.println("关闭流异常:"+e2.getMessage());
            	e2.printStackTrace();
            }  
        }  
        return result;  
    }

    public static void main(String[] args) {

        String url="https://ntf-oc.hb-vpc-prod.bwae.org/open-api/handle/auth";
        String appid="GNXon3g8";
        String appkey="b3XAmvvLJtRRks7NBQScfQLcC6fXMx";
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String format1 = format.format(new Date());
        Map<String,String> jsonParam = new HashMap<>();
        jsonParam.put("cardId", "8986031948208593235");
        jsonParam.put("cardType","0");
        String s = sendPost(url, JSON.toJSONString(jsonParam), format1, appid, appkey);

        Map<String,Object> object = JSON.parseObject(s);
        Map<String,Object> respStatus = (Map<String, Object>) object.get("respStatus");
        if (respStatus.get("code").equals("0000")){
            Map<String,Object> respBody = (Map<String, Object>) object.get("respBody");
            Boolean flag = (Boolean) respBody.get("flag");
            if (flag){
                System.out.println("sss");
            }else {

            }
        }else {

        }
    }

}
